package com.web.dao.remark;

import com.mysql.cj.protocol.Resultset;
import com.web.bean.remark.Answer;
import com.web.bean.remark.Remark;
import com.web.result.Result;
import com.web.utils.DBUtil;
import com.web.utils.DateUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @autor Chen
 * @create 2021--25-13:07
 * @desc
 */
public class RemarkDaoImpl implements RemarkDao {
    private Connection connection;

    public RemarkDaoImpl(Connection connection) {
        this.connection = connection;
    }

    @Override
    /*查找所有的评论*/
    public List<Remark> selectRemarksForChapter(String nid,String chapterid) throws Exception {
        List<Remark> remarks=new ArrayList<>();
        String sql="select * from remark where nid=? and chapterid=?";
            PreparedStatement statement=connection.prepareStatement(sql);
            statement.setString(1,nid);
            statement.setString(2,chapterid);
            ResultSet res=statement.executeQuery();
            while (res.next()){
                Remark remark=new Remark();
                remark.setRid(res.getString(1));
                remark.setNid(res.getString(2));
                remark.setUid(res.getString(3));
                remark.setRmtime(res.getDate(4));
                remark.setContent(res.getString(5));
                remark.setChapterid(res.getString(6));
                remark.setType(res.getString(7));
                remarks.add(remark);
            }
            System.out.println(remarks);
            connection.close();
        return remarks;
    }

    @Override
    /*根据章节id查找*/
    public Remark selectRemarkForChapterById(String id) throws Exception {
        Remark remark=new Remark();
        String sql="select * from remark where rid=?";
            PreparedStatement statement=connection.prepareStatement(sql);
            statement.setString(1,id);
            ResultSet res=statement.executeQuery();
            while (res.next()){
                remark.setRid(res.getString(1));
                remark.setNid(res.getString(2));
                remark.setUid(res.getString(3));
                remark.setRmtime(res.getDate(4));
                remark.setContent(res.getString(5));
                remark.setChapterid(res.getString(6));
                remark.setType(res.getString(7));
                remark.setReplyCount(res.getString(8));
            }
            connection.close();
        return remark;
    }

    /*传入Remark对象，根据对象的rid修改评论content*/
    @Override
    public void updateRemarkForChapter(Remark remark) throws Exception {
        String sql="update remark set content=? where rid=?";
            PreparedStatement statement=connection.prepareStatement(sql);
            statement.setString(1,remark.getContent());
            statement.setString(2,remark.getRid());
            statement.executeUpdate();
            connection.close();
    }

    @Override
    public void addRemarkForChapter(Remark remark) throws  Exception {
        System.out.println("add1");
        String sql="insert into remark values(?,?,?,?,?,?,?,?)";
        PreparedStatement statement=connection.prepareStatement(sql);
        statement.setString(1,remark.getRid());
        statement.setString(2,remark.getNid());
        statement.setString(3,remark.getUid());
        statement.setTimestamp(4, DateUtil.parseTimestamp(remark.getRmtime()));
        statement.setString(5,remark.getContent());
        statement.setString(6,remark.getChapterid());
        statement.setString(7,remark.getType());
        statement.setString(8,remark.getReplyCount());
        statement.executeUpdate();
        connection.close();
    }


    /*更具评论id删除评论*/
    @Override
    public void deleteRemarkForChapter(String id) throws Exception {
        String sql="delete from remark where rid=?";
            PreparedStatement statement=connection.prepareStatement(sql);
            statement.setString(1,id);
            statement.executeUpdate();
            connection.close();
    }

    @Override
    public List<Remark> selectRemarksForNovel() throws Exception {
        return null;
    }

    @Override
    public List<Remark> selectRemarkForNovelById(String id) throws Exception {
        String sql="select * from remark where nid=? and type = 2";
        List<Remark> remarks=new ArrayList<>();
            PreparedStatement statement=connection.prepareStatement(sql);
            statement.setString(1,id);
            ResultSet res=statement.executeQuery();
            while (res.next()){
                Remark remark=new Remark();
                remark.setRid(res.getString(1));
                remark.setNid(res.getString(2));
                remark.setUid(res.getString(3));
                remark.setRmtime(res.getDate(4));
                remark.setContent(res.getString(5));
                remark.setChapterid(res.getString(6));
                remark.setType(res.getString(7));
                remark.setReplyCount(res.getString(8));
                remarks.add(remark);
            }
            connection.close();
        return remarks;
    }

    @Override
    public void updateRemarkForNovel(Remark remark) throws Exception {
        String sql="update remark set content=? where rid=?";
        PreparedStatement statement=connection.prepareStatement(sql);
        statement.setString(1,remark.getContent());
        statement.setString(2,remark.getRid());
        statement.executeUpdate();
        connection.close();
    }

    @Override
    public void addRemarkForNovel(Remark remark) throws Exception {
        System.out.println("add2");
        String sql="insert into remark values(?,?,?,?,?,?,?,?)";
            PreparedStatement statement=connection.prepareStatement(sql);
            statement.setString(1,remark.getRid());
            statement.setString(2,remark.getNid());
            statement.setString(3,remark.getUid());
            statement.setTimestamp(4, DateUtil.parseTimestamp(remark.getRmtime()));
            statement.setString(5,remark.getContent());
            statement.setString(6,remark.getChapterid());
            statement.setString(7,remark.getType());
            statement.setString(8,remark.getReplyCount());
            statement.executeUpdate();
            connection.close();
    }

    @Override
    public void deleteRemarkForNovel(String nid) throws Exception {
        String sql="delete from remark where nid =?";
            PreparedStatement statement=connection.prepareStatement(sql);
            statement.setString(1,nid);
            statement.executeUpdate();
            connection.close();
    }

    @Override
    public List<String> selectAllRid() throws Exception {
        List<String> rids =new ArrayList<>();
        String rid="";
        String sql="select rid from remark";
        PreparedStatement statement=connection.prepareStatement(sql);
        ResultSet res=statement.executeQuery();
        while (res.next()){
            rid=res.getString(1);
            rids.add(rid);
        }
        connection.close();
        return rids;
    }
    public void deleteRemarkById(String rid) throws Exception{
        System.out.println(rid);
        String sql="delete from remark where rid=?";
        PreparedStatement statement=connection.prepareStatement(sql);
        statement.setString(1,rid);
        statement.executeUpdate();
        connection.close();
    }

    @Override
    public List<String> selectAllAid() throws Exception {
        String sql="select aid from answers";
        ArrayList<String> aids=new ArrayList<>();
        PreparedStatement statement=connection.prepareStatement(sql);
        ResultSet res=statement.executeQuery();
        while (res.next()){
            aids.add(res.getString(1));
        }
        connection.close();
        return aids;
    }

    @Override
    public void addReply(Answer answer) throws Exception {
        String sql="insert into answers values(?,?,?,?)";
        Connection connection_1= DBUtil.getInstance().getConnection();
        PreparedStatement statement=connection_1.prepareStatement(sql);
        statement.setString(1,answer.getAid());
        statement.setString(2,answer.getRid());
        statement.setTimestamp(3,DateUtil.parseTimestamp(answer.getCreateTime()));
        statement.setString(4,answer.getReplyContent());
        statement.executeUpdate();
        connection.close();
    }
    /*查找replyContent*/

    @Override
    public Integer selectReplyCount(String rid) throws Exception {
        String sql="select replyCount from remark where rid=?";
        PreparedStatement statement=connection.prepareStatement(sql);
        statement.setString(1,rid);
        ResultSet res=statement.executeQuery();
        List<String> replyCount=new ArrayList<>();
        while (res.next()){
            replyCount.add(res.getString(1));
        }
        String replycount=replyCount.get(0);
        Integer new_ReplyCount=Integer.parseInt(replycount);
        connection.close();
        return new_ReplyCount+1;

    }
    /*新增回复后插入reply++*/

    @Override
    public void updateReplyCount(String rid,String replyCount) throws Exception {
        String sql="update remark set replyCount =? where rid=?";
        Connection connection_1= DBUtil.getInstance().getConnection();
        PreparedStatement statement=connection_1.prepareStatement(sql);
        statement.setString(1,replyCount);
        statement.setString(2,rid);
        statement.executeUpdate();
        connection.close();
    }

    @Override
    public List<Answer> selectAnswer(String rid) throws Exception {
        String sql="select * from answers where rid=?";
        List<Answer> answers=new ArrayList<>();
        PreparedStatement statement=connection.prepareStatement(sql);
        statement.setString(1,rid);
        ResultSet res=statement.executeQuery();
        while (res.next()){
            Answer answer=new Answer();
            answer.setAid(res.getString(1));
            answer.setRid(res.getString(2));
            answer.setCreateTime(res.getDate(3));
            answer.setReplyContent(res.getString(4));
            answers.add(answer);
        }
        connection.close();
        return answers;
    }
}
